package bman.game.engine.client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bman.game.engine.Controller;
import bman.game.engine.Entity;
import bman.game.engine.GameConsole;
import bman.game.engine.GameMap;
import bman.game.engine.GraphicsEngine;

/**
 * STATUS OF THIS IS UNKNOWN.
 * This was used during testing.
 * 
 * @author MrJacky
 *
 */
public class StandardFrame2 extends JFrame implements KeyListener, ActionListener
{
	Logger log = Logger.getLogger(StandardFrame2.class.getName());
	
	GameMap map;
	Entity me;
	Controller controller;
	GameConsole console;
	
	public StandardFrame2() throws Exception
	{
		setLayout(new BorderLayout());
		initMenu(this);
		
		setSize(625,425);	
		setLocation(500,500);
		setVisible(true);
		setFocusable(true);
		addKeyListener(this);
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
		
		console = new GameConsole();
		GraphicsEngine p = console.getGraphicsEngine();
		getContentPane().add(p,BorderLayout.CENTER);
		getContentPane().validate();
	}
	
	JTextField ipText;
	JTextField portText;
	
	public void initMenu(JFrame frame)
	{
		JPanel p = new JPanel();
		
		JButton connect = new JButton("Connect");
		connect.addActionListener(this);
		
		ipText = new JTextField("127.0.0.1:777");
		portText = new JTextField("888");
		
		p.add(connect);
		p.add(ipText);
		p.add(portText);
		
		frame.getContentPane().add(p,BorderLayout.NORTH);
		
		Component[] c = this.getComponents();
		for(int i = 0; i < c.length; i++)
		{
			c[i].setFocusable(false);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		String cmd = e.getActionCommand();
	    System.out.println("Source: "+cmd);
	     
	    try
		{
			if("Connect".equalsIgnoreCase(cmd))
			{
				console.setPort(Integer.parseInt(portText.getText()));
				console.connect(ipText.getText());
			}
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
		}

	}

	public void keyPressed(KeyEvent ke)
	{
		System.out.println("KeyPressed: "+ke.getKeyCode());
		controller.perform(ke);
	}
	
	public void keyReleased(KeyEvent ke)
	{
	}
	
	public void keyTyped(KeyEvent ke)
	{
	}

	public static void main(String[] args)
	{
		try
		{
			StandardFrame2 sf = new StandardFrame2();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
